package javaproxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * jdk动态代理（只能代理实现了接口的）
 */
public class StudentProxy {
    public static void main(String[] args) {
        StudentController studentController = new StudentController();
        IStudent student = (IStudent) Proxy.newProxyInstance(StudentController.class.getClassLoader(), StudentController.class.getInterfaces(), new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                System.out.println("你正在访问" + method.getName() + "方法");
                long l = System.currentTimeMillis();
                Object invoke = method.invoke(studentController,args);
                long l1 = System.currentTimeMillis();
                System.out.println("使用方法花费的时间" + (l1 - l));
                return invoke;
            }
        });

        student.add();
        student.delete();
        student.find();
        student.update();




    }


}
